package com.jcfk.eam.service.main;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jcfk.common.business.service.BaseService;
import com.jcfk.common.core.utils.BeanUtils;
import com.jcfk.common.security.utils.SecurityUtils;
import com.jcfk.common.web.domain.RetResult;
import com.jcfk.eam.dao.main.EamAssetPictureDao;
import com.jcfk.eam.domain.dto.main.EamAssetPictureDTO;
import com.jcfk.eam.domain.po.main.EamAssetPicture;
import com.jcfk.eam.domain.query.main.EamAssetPictureQuery;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.jcfk.common.core.utils.BeanUtils.convertList;

/**
 * 危险作业附件
 *
 * @author libinghai
 * @email 35738025@gqq.com
 * @date 2024-07-12
 */
@Service
public class EamAssetPictureService extends BaseService<EamAssetPictureDao, EamAssetPicture> {

    /**
     * 判断SsmTaskInspectionAttach是已经存在
     *
     * @param dto
     * @return
     */
    public RetResult<String> isValid(EamAssetPictureDTO dto) {
        QueryWrapper<EamAssetPicture> queryWrapper = new QueryWrapper<>();

        return RetResult.ok();
    }

    /**
     * 插入危险作业附件
     *
     * @param dto 插入对象
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public void insert(EamAssetPictureDTO dto) {

        // 保存
        EamAssetPicture item = new EamAssetPicture();
        BeanUtils.copyProperties(dto, item);
        this.setDefaultValue(item);

        super.save(item);
    }

    /**
     * 更新危险作业附件
     *
     * @param dto 更新对象
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public void update(EamAssetPictureDTO dto) {

        // 保存
        EamAssetPicture item = new EamAssetPicture();
        BeanUtils.copyProperties(dto, item);

        // 默认数据
        this.setDefaultValue(item);

        super.updateById(item);
    }

    /**
     * 保存危险作业附件列表
     * @param dtoList
     */
    public void save(List<EamAssetPictureDTO> dtoList){
        List<EamAssetPicture> list=new ArrayList<>();

        for(EamAssetPictureDTO dto : dtoList){
            EamAssetPicture item = new EamAssetPicture();
            BeanUtils.copyProperties(dto, item);

            // 默认数据
            this.setDefaultValue(item);
            list.add(item);
        }

        this.saveBatch(list);
    }

    /**
     * 设置添加默认值
     *
     * @param item 添加对象
     */
    public void setDefaultValue(EamAssetPicture item) {
        if (StrUtil.isEmpty(item.getCreatedUserId())) {
            item.setCreatedUserId(SecurityUtils.getUserId());
            item.setCreateTime(new Date());
            item.setVersion(1L);
            item.setCreatedUserName(SecurityUtils.getUserName());
        }
        item.setLastUpdatedUserId(SecurityUtils.getUserId());
        item.setLastUpdateTime(new Date());
        item.setLastUpdatedUserName(SecurityUtils.getUserName());
    }

    /**
     * 根据主键获取危险作业附件信息
     *
     * @param id 主键
     * @return StandardCodeDTO
     */
    public EamAssetPictureDTO get(String id) {
        // 查询数据
        EamAssetPicture item = this.baseMapper.selectById(id);
        EamAssetPictureDTO dto = new EamAssetPictureDTO();
        try {
            BeanUtils.copyProperties(item, dto);
        } catch (RuntimeException e) {
            throw e;
        }

        return dto;
    }


    /**
     * 根据条件获取单表信息列表
     *
     * @param query 查询条件
     * @return List<SsmTaskInspectionAttachDTO>
     */
    public List<EamAssetPictureDTO> getList(EamAssetPictureQuery query) {
        // 查询条件
        QueryWrapper<EamAssetPicture> qw = new QueryWrapper<>();
        qw.lambda().eq(!StrUtil.isEmpty(query.getAssetId()),EamAssetPicture::getAssetId,query.getAssetId());

        // 查询数据
        List<EamAssetPicture> list = super.list(qw);

        return convertList(list, EamAssetPictureDTO.class);
    }

    /**
     * 删除列表
     * @param query
     * @return
     */
    public Integer deleteList(EamAssetPictureQuery query){
        // 删除条件
        QueryWrapper<EamAssetPicture> qw = new QueryWrapper<>();
        qw.lambda().eq(!StrUtil.isEmpty(query.getAssetId()),EamAssetPicture::getAssetId,query.getAssetId());
        Integer count=this.baseMapper.delete(qw);

        return count;
    }

    /**
     * 根据主键获取危险作业附件信息
     *
     * @param id 主键
     * @return StandardCodeDTO
     */
    public void deleteItem(String id) {
        this.removeById(id);
    }
}

